const async = require('async');
const mysql = require('../../utils/db').mysql;
const qiniu = require('../../utils/qiniu');
exports.page = function(req, res, next){
    const userInfo = req.session.userInfo;
    const qiniuToken = qiniu .getToken();
    req.session.touch();
    if(userInfo.flowerAuthority != 1){
        res.statusCode = 404;
        res.send('404');
        return;
    }
    res.render('admin/addFlower', {
        userInfo: userInfo,
        channel: {
            type: 'manage-huayu',
            index: 1
        },
        qiniuToken: qiniuToken
    });
}
exports.addFlower = function(req, res, next){
    const userInfo = req.session.userInfo;
    if(userInfo.flowerAuthority != 1){
        res.send({
            code: 400,
            msg: '添加文章失败'
        });
        return;
    }
    const data = {
        title: req.body.title,
        thumb: req.body.thumb,
        desc: req.body.desc,
        createtime: new Date(),
        content: req.body.content
    }
    const queryStr = 'INSERT INTO huayu (title, thumb, createtime, hydesc, content) VALUES (?,?,?,?,?)';
    const queryArr = [data.title, data.thumb, data.createtime, data.desc, data.content];

    mysql.getConnection(function(err, conn) {
        conn.query(queryStr, queryArr, function (err, results) {
            if (err) {
                console.log('Error:' + err, '========addUser=======');
                res.send({
                    code: 400,
                    msg: '添加文章失败'
                });
            } else {
                res.send({
                    code: 200,
                    msg: '添加文章成功'
                });
            };
            mysql.releaseConnection(conn);
        });
    })
}